#!/usr/bin/env python

import sys, os
from banner_class import Banner 

def _create_dataset ( start_point, lines, rows, tot_rows ):
	#sys.stderr.write ( "CREATE DATASET\n" )
        res = {}

        count = 0
        for v in rows:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'rows' ] = tot_rows
        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = lines

        return res

def get_templates ( liwe ):
	"""
        :INPUT:
	"""
	n = Banner ( liwe )
	return { "templates" : n.get_templates () }

def list_banner ( liwe, id_gruppo = 0, id_tipo = 0, data = '', giorni = 0 , descr = '', _X_START_POINT = 0, _X_LINES = 10, _X_PAGE = 0, do_count = 0 ):
	"""
	:INPUT:
	- id_gruppo:	SAFE_NUMBER
	- id_tipo:		SAFE_NUMBER
	- data:			SAFE_STRING
	- giorni:		SAFE_NUMBER
	- descr:		SAFE_DB_STRING
	- _X_LINES:		SAFE_NUMBER
	- _X_START_POINT:	SAFE_NUMBER
	- _X_PAGE:		SAFE_NUMBER
	- do_count:		SAFE_NUMBER
	"""
	use_dataset = 0
	quant = -1

	if _X_LINES != -1:
		quant = _X_LINES
		use_dataset = 1

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * quant

	b = Banner ( liwe )
	res = b.list_banner ( id_gruppo = id_gruppo, id_tipo = id_tipo, data = data, giorni = giorni, descr = descr, quant = quant, start_point = start_point, do_count = use_dataset )

	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]

	if not use_dataset:
		if not rows: rows = []
		return { 'rows': rows }
		
	return _create_dataset ( start_point, quant, rows, tot_rows )
	
def get_banner ( liwe, id_banner ):
	"""
	:INPUT:
		- id_banner:	SAFE_NUMBER
	"""
	b = Banner ( liwe )
	res = b.get_banner ( id_banner = id_banner )
	return { 'banner': res }

def get_gruppo ( liwe, id_gruppo = 0 , name = '' ):
	"""
	:INPUT:
		- id:	SAFE_NUMBER
		- name	SAFE_DB_STRING
	"""
	b = Banner ( liwe )
	res = b.get_gruppo ( id_gruppo = id_gruppo, name = name )
	return { 'gruppo': res }

def list_gruppo ( liwe, _X_START_POINT = 0, _X_LINES = 10, _X_PAGE = 0, do_count = 0 ):
        """
        :INPUT:
                - _X_LINES:		SAFE_NUMBER
                - _X_START_POINT:	SAFE_NUMBER
                - _X_PAGE:		SAFE_NUMBER
		- do_count:		SAFE_NUMBER
        """
        use_dataset = 0
	quant = -1

        if _X_LINES != -1:
                quant = _X_LINES
                use_dataset = 1

        if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * quant

        b = Banner ( liwe )
	res = b.list_gruppo ( quant = quant, start_point = start_point, do_count = use_dataset )

        rows = res [ 'rows' ]
        tot_rows = res [ 'tot_rows' ]

        if not use_dataset:
                if not rows: rows = []
                return { 'rows': rows }

        return _create_dataset ( start_point, quant, rows, tot_rows )


def get_tipo ( liwe, id_tipo = 0 , name = '' ):
	"""
	:INPUT:
		- id_tipo:	SAFE_NUMBER
		- name:		SAFE_DB_STRING
	"""
	b = Banner ( liwe )
	res = b.get_tipo ( id_tipo = id_tipo, name = name )
	return { 'tipo': res }

def list_tipo ( liwe, _X_START_POINT = 0, _X_LINES = 10, _X_PAGE = 0, do_count = 0 ):
	"""
	:INPUT:
	- _X_LINES:		SAFE_NUMBER
	- _X_START_POINT:	SAFE_NUMBER
	- _X_PAGE:		SAFE_NUMBER
	- do_count:		SAFE_NUMBER
	"""
	use_dataset = 0
	quant = -1

	if _X_LINES != -1:
			quant = _X_LINES
			use_dataset = 1

	if _X_START_POINT:
			start_point = _X_START_POINT
	else:
			start_point = _X_PAGE * quant

	b = Banner ( liwe )
	res = b.list_tipo ( quant = quant, start_point = start_point, do_count = use_dataset )

	rows = res [ 'rows' ]
	tot_rows = res [ 'tot_rows' ]

	if not use_dataset:
		if not rows: rows = []
		return { 'rows': rows }

	return _create_dataset ( start_point, quant, rows, tot_rows )



def show_banner ( liwe , last_id = 0, id_tipo = 0, tipo = '' ):
	"""
	:INPUT:
		- last_id:	SAFE_NUMBER
		- id_tipo:	SAFE_NUMBER
		- tipo:		SAFE_DB_STRING
	"""
	b = Banner ( liwe )
	
	res = b.show_banner ( last_id = last_id, id_tipo = id_tipo, tipo = tipo )

	return { 'banner' : res }

def load_banner ( liwe, id_tipo = 0, tipo = '', tmpl = '' ):
	"""
	:INPUT:
		- id_tipo:	SAFE_NUMBER
		- tipo:		SAFE_DB_STRING
		- tmpl:		SAFE_DB_STRING
	"""
	b = Banner ( liwe )	
	return b.load_banner ( id_tipo = id_tipo, tipo = tipo, tmpl = tmpl )

def click_banner ( liwe, id_banner = 0 ):
	"""
	:INPUT:
		- id_banner: SAFE_NUMBER
	"""

	b = Banner ( liwe )
	banner = b.get_banner ( id_banner = id_banner )

	if not banner: return
	liwe.invoke ( "stats.ajax.log", module = 'banner' , id_ref = id_banner, event = 'click', uid = liwe.session.data.get ( "uid", 0 ) ) 

	if not banner [ 'lnk' ]:  return
	liwe.pyhp.redirect ( banner [ 'lnk' ] )
